package com.chenfan.process.entity.po;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.ccp.util.tool.UuidUtil;
import com.chenfan.infra.context.UserContextHolder;
import com.chenfan.process.entity.dto.ApprovalDataTransferDTO;
import com.chenfan.process.entity.vo.ProcessTransferLogVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.Date;

/**
 * @author liuming
 * @date 2021/10/26
 */
@Data
@TableName(value = "process_transfer_log")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProcessTransferLogPo {

    /**
     * 主键ID
     */
    @TableId
    @ApiModelProperty("主键ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    /**
     * 转移状态 0-进行中 1-转移成功 2-转移失败
     */
    @ApiModelProperty("转移状态")
    private Integer status;

    @JsonSerialize(using = ToStringSerializer.class)
    /**
     * 操作用户ID
     */
    @ApiModelProperty("操作用户ID")
    private Long operatedBy;

    /**
     * 操作用户名称
     */
    @ApiModelProperty("操作用户名称")
    private String operatedName;


    /**
     * 转移源用户ID
     */
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty("转移源用户ID")
    private Long sourceUserId;

    /**
     * 转移源用户名称
     */
    @ApiModelProperty("转移源用户名称")
    private String sourceUserName;

    /**
     * 转移目标用户ID
     */
    @ApiModelProperty("转移目标用户ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long targetUserId;

    /**
     * 转移目标名称
     */
    @ApiModelProperty("转移目标名称")
    private String targetUserName;

    /**
     * 业务模板
     */
    @ApiModelProperty("业务模板")
    private String businessTemplate;

    @ApiModelProperty("转移类型")
    private Integer transferType;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    /**
     * 更新时间
     */
    @ApiModelProperty("更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;

    /**
     * 转移数据量
     */
    @ApiModelProperty("转移数据量")
    private Long dataCount;

    public static ProcessTransferLogPo ofDefault(ApprovalDataTransferDTO data) {
        ProcessTransferLogPo log = new ProcessTransferLogPo();
        log.setId(UuidUtil.generateId());
        log.setOperatedBy(UserContextHolder.getUserVo().getUserId());
        log.setOperatedName(UserContextHolder.getUserVo().getRealName());
        log.setSourceUserId(data.getSourceUserId());
        log.setSourceUserName(data.getSourceUserName());
        log.setTargetUserId(data.getTargetUserId());
        log.setTargetUserName(data.getTargetUserName());
        return log;
    }

    @Data
    public static class Constants {

        private Constants() {}

        public static final String OPERATED_NAME = "operated_name";
        public static final String CREATE_TIME = "create_time";
        public static final String STATUS = "status";
        public static final String TRANSFER_TYPE = "transfer_type";
    }

    public ProcessTransferLogVO copyProcessTransferPo() {
        ProcessTransferLogVO po = new ProcessTransferLogVO();
        BeanUtils.copyProperties(this, po);
        return po;
    }

}
